package thread;

import java.util.Random;

public class practice2 {
    private static final int ARRAY_SIZE = 10_000_000; // 1000万长度
    private static int[] array = new int[ARRAY_SIZE];
    private static long evenSum = 0; // 偶数下标和
    private static long oddSum = 0;  // 奇数下标和

    public static void main(String[] args) throws InterruptedException {
        Random random = new Random();
        for (int i = 0; i < ARRAY_SIZE; i++) {
            array[i] = random.nextInt(100) + 1;
        }

        Thread t = new Thread(()->{
            for (int x:
                 array) {
                if(x % 2 == 0){
                    evenSum += x;
                }
            }
        });

        long time1 = System.currentTimeMillis();

        t.start();

        for (int x:
                array) {
            if(x % 2 != 0){
                oddSum += x;
            }
        }

        t.join();

        long Sum = oddSum + evenSum;

        long time2 = System.currentTimeMillis();

        System.out.println("执行时间"+(time2-time1));
        System.out.println("偶数下标和: " + evenSum);
        System.out.println("奇数下标和: " + oddSum);
        System.out.println("总和: " + Sum);
    }
}
